package leetcode_周赛._2022._202206;

import java.util.Arrays;

/**
 * @author yzh
 * @version 1.0
 * @date 2022/6/12 10:46
 * 297 场力扣周赛
 */
public class _12 {

    public double calculateTax(int[][] brackets, int income) {
        if (income == 0) return 0;
        if (brackets[0][0] > income) return (((double) brackets[0][1]) * income / 100);
        double ans = 0;
        ans += (((double) brackets[0][0]) * brackets[0][1] / 100);
        for (int i = 1; i < brackets.length; i++) {
            int tmp = brackets[i][0] - brackets[i - 1][0];
            int tmp2 = income - brackets[i - 1][0];
            if (tmp > tmp2) {
                ans += (((double) tmp2) * brackets[i][1] / 100);
                break;
            }
            ans += (((double) tmp) * brackets[i][1] / 100);
        }
        return ans;
    }

    public static void main(String[] args) {
        System.out.println(new _12().minPathCost(new int[][]{{5, 3}, {4, 0}, {2, 1}},
                new int[][]{{9, 8}, {1, 5}, {10, 12}, {18, 6}, {2, 4}, {14, 3}}));
    }

    // 5270. 网格中的最小路径代价
    public int minPathCost(int[][] grid, int[][] moveCost) {
        int m = grid.length, n = grid[0].length;
        // dp[i][j] 表示第一行到第 i 行第 j 列所需的最小路径
        int[][] dp = new int[m][n];
        for (int i = 0; i < m; i++) Arrays.fill(dp[i], Integer.MAX_VALUE);
        for (int i = 0; i < n; i++) dp[0][i] = grid[0][i];
        for (int i = 1; i < m; i++) {
            for (int j = 0; j < n; j++) {
                for (int k = 0; k < m; k++)
                    dp[i][j] = Math.min(dp[i - 1][k] + moveCost[grid[i - 1][k]][j] + grid[i][j], dp[i][j]);
            }
        }
        int ans = Integer.MAX_VALUE;
        for (int i = 0; i < n; i++) ans = Math.min(dp[m - 1][i], ans);
        return ans;
    }

}
